package tests;

import common.Utils;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

import static org.openqa.selenium.support.ui.ExpectedConditions.textToBe;


public class ListPage extends Utils{
    public static String url = "http://127.0.0.1:9580/index.html";

    public ListPage() {
        super(url);
    }

    //检查页面各个元素是否加载成功
    public void ListByLoging() throws InterruptedException {

        //通过页面标题来检查是否成功
        String title = driver.getTitle();
        assert title.equals("比特论坛");
        //通过查看比特论坛logo是否登录成功
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > h1 > a > img"));
        //通过查看发布帖子是否登录成功
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post"));
        //昼夜模式测试
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-dark > svg")).click();
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-light")).click();
        //输入框测试
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.d-none.d-md-flex.me-3 > div > form > div > input"));

        //信息栏
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg"));

        //个人信息模块
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();

        //不同模块点击,点击java模块
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(2) > a > span.nav-link-title")).click();
        //查看java模块第一个帖子是否存在来校验是否切换模块成功
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div"));
    }


    //处理私信成功
    public void MessageSuc() throws InterruptedException {
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg")).click();
        //等待页面加载
        //Thread.sleep(4000);
        //点击消息
        driver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a")).click();
        //点击回复
        driver.findElement(By.cssSelector("#btn_index_message_reply")).click();
        //输入回复信息
        WebDriverWait foo = new WebDriverWait(driver, Duration.ofSeconds(3));
        Thread.sleep(2000);
        foo.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#index_message_reply_receive_content"))).sendKeys("越努力越幸运！");

        //Thread.sleep(3000);
        // driver.findElement(By.cssSelector("#index_message_reply_receive_content")).sendKeys("越努力越幸运！");
        //发送
        driver.findElement(By.cssSelector("#btn_index_send_message_reply")).click();
        //检验发送成功
        String sendSuc = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert sendSuc.equals("成功");
        //关闭信息页面
        driver.findElement(By.cssSelector("#index_message_offcanvasEnd > div.offcanvas-header > button")).click();

    }

    //处理私信失败
    public void MessageFail() throws InterruptedException, IOException {
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg")).click();
        //等待页面加载
        //Thread.sleep(4000);
        //点击消息
        driver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a")).click();
        //点击回复
        driver.findElement(By.cssSelector("#btn_index_message_reply")).click();
        //输入回复信息
        driver.findElement(By.cssSelector("#index_message_reply_receive_content"));
        //发送
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("#btn_index_send_message_reply")).click();
        //检验发送结果
        //显示等待
        WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(3));
        boolean send = wait.until(ExpectedConditions.textToBe(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2"),"警告"));
        if(send){
            getScreenShot(getClass().getName());
        }else {
            System.out.println("发送成功");
        }
       /*  String sendFail = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        getScreenShot(getClass().getName());
        assert sendFail.equals("警告");
*/
        //显示等待关闭回复框
        /*WebDriverWait foo = new WebDriverWait(driver, Duration.ofSeconds(10));
        Thread.sleep(2000);
        foo.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#index_message_reply_modal > div > div > div.modal-header > button"))).click();
*/
        //关闭信息页面
        WebDriverWait closeWait = new WebDriverWait(driver, Duration.ofSeconds(10));
        closeWait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#index_message_offcanvasEnd > div.offcanvas-header > button"))).click();

    }

    //发布帖子成功
    public void PublishPostSuc() throws IOException {
        //点击页面发布帖子按钮
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        //板块
        driver.findElement(By.cssSelector("#article_post_borad")).click();
        driver.findElement(By.cssSelector("#article_post_borad>option:nth-child(1)")).click();
        //标题
        driver.findElement(By.cssSelector("#article_post_title")).sendKeys("测试01");
        //内容
        driver.findElement(By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > li:nth-child(20) > a > i")).click();
        //发布
  /*      WebDriverWait publishWait = new WebDriverWait(driver, Duration.ofSeconds(50));
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        publishWait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#article_post_submit"))).click();*/
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        driver.findElement(By.cssSelector("#article_post_submit")).click();
        //校验发布成功
        String pubSuc = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert pubSuc.equals("成功");
    }

    //发布帖子失败
    public void PublishPostFail() throws IOException {
        //点击页面发布帖子按钮
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        //板块
        driver.findElement(By.cssSelector("#article_post_borad")).click();
        driver.findElement(By.cssSelector("#article_post_borad>option:nth-child(1)")).click();
        //标题
        driver.findElement(By.cssSelector("#article_post_title"));
        //内容
        driver.findElement(By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > li:nth-child(20) > a > i")).click();
        //发布
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        driver.findElement(By.cssSelector("#article_post_submit")).click();
       /* WebDriverWait publishWait = new WebDriverWait(driver, Duration.ofSeconds(10));
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        publishWait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#article_post_submit"))).click();*/
        //校验提示发布失败
        String pubFail = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        //截图保存测试结果验证警告提示内容是否与预期一致
        getScreenShot(getClass().getName());
        assert pubFail.equals("提示");

    }

    //显示个人帖子
    public void ShowPost(){
        //点击用户
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        //点击我的帖子
        driver.findElement(By.cssSelector("#index_user_profile")).click();
        //查看当前用户昵称是否存在来校验页面是否加载成功
        driver.findElement(By.cssSelector("#profile_nickname"));
        //点击当前用户的第一篇帖子
        driver.findElement(By.cssSelector("#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-truncate > a")).click();
    }

    //点赞
    public void LickCount(){
        ShowPost();
        driver.findElement(By.cssSelector("#details_btn_like_count")).click();
        String likeCountSuc = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert likeCountSuc.equals("成功");

    }

    //编辑成功
    public void EditSuc(){
        ShowPost();
        //点击当前帖子页面的编辑按钮
        driver.findElement(By.cssSelector("#details_artile_edit")).click();
        //清空原标题
        driver.findElement(By.cssSelector("#edit_article_title")).clear();
        //修改标题
        driver.findElement(By.cssSelector("#edit_article_title")).sendKeys("修改标题01");
        //点击提交
        driver.findElement(By.cssSelector("#edit_article_submit")).click();

        String editSuc = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert editSuc.equals("成功");
    }

    //编辑失败
    public void EditFail() throws IOException {
        ShowPost();
        //点击当前帖子页面的编辑按钮
        driver.findElement(By.cssSelector("#details_artile_edit")).click();
        //清空原标题
        driver.findElement(By.cssSelector("#edit_article_title")).clear();
        //点击提交
        driver.findElement(By.cssSelector("#edit_article_submit")).click();

        String editSuc = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        getScreenShot(getClass().getName());
        assert editSuc.equals("提示");
    }

    //删除成功
    public void DeleteSuc(){
        ShowPost();
        //点击当前帖子页面的删除按钮
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a")).click();
        //点击删除
        driver.findElement(By.cssSelector("#details_delete_modal > div > div > div.modal-footer > div > div > div:nth-child(2)>a")).click();
        String deleteSuc = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert deleteSuc.equals("成功");
    }

    //删除失败
    public void DeleteFail(){
        ShowPost();
        //点击当前帖子页面的删除按钮
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a")).click();
        //点击取消
        driver.findElement(By.cssSelector("#details_delete_modal > div > div > div.modal-footer > div > div > div:nth-child(1) > a")).click();

    }

    //回复
    public void RepleSuc(){
        ShowPost();
        //在回复框中输入内容
        driver.findElement(By.cssSelector("#article_details_reply > div.editormd-toolbar > div > ul > li:nth-child(20) > a")).click();
        //点击回复按钮
        driver.findElement(By.cssSelector("#details_btn_article_reply")).click();
        String replySuc = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert replySuc.equals("提示");
    }

    //回复失败
    public void RepleFail() throws IOException {
        ShowPost();
        //当回复框为空没有内容时直接点击回复按钮
        driver.findElement(By.cssSelector("#details_btn_article_reply")).click();
        String replyFail = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        getScreenShot(getClass().getName());

        assert replyFail.equals("提示");
    }


    //个人中心
    public void PersonalCenter(){
        //点击用户
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        //点击个人中心
        driver.findElement(By.cssSelector("#index_user_settings")).click();
        //通过查看头像是否存在来校验页面是否加载成功
        driver.findElement(By.cssSelector("#settings_avatar"));
    }

    //个人中心修改失败01---修改密码，原密码错误，新密码和确认密码一致
    public void PersonalCenterModifyFail01() throws IOException {
        PersonalCenter();
        //输入原密码
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("0");
        //输入新密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("123");
        //输入确认密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("123");
        //提交修改
        //driver.findElement(By.cssSelector("#settings_submit_password")).click();
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > div:nth-child(14) > div > div > form > div.form-footer>button")).click();
        String modifyFail01 = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        getScreenShot(getClass().getName());
        assert modifyFail01.equals("警告");
        //刷新页面
        driver.navigate().refresh();
    }

    //个人中心修改失败02---修改密码，原密码正确，新密码和确认密码不一致
    public void PersonalCenterModifyFail02() throws IOException {
        PersonalCenter();
        //输入原密码
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("0");
        //输入新密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("123");
        //输入确认密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("123");
        //提交修改
        //driver.findElement(By.cssSelector("#settings_submit_password")).click();
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > div:nth-child(14) > div > div > form > div.form-footer>button")).click();
        String modifyFail02 = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        getScreenShot(getClass().getName());
        assert modifyFail02.equals("提示");
        //刷新页面
        driver.navigate().refresh();
    }

    //个人中心修改失败03---未修改简介内容，确点击修改按钮
    public void PersonalCenterModifyFail03() throws IOException {
        driver.findElement(By.cssSelector("#settings_submit_remark")).click();
        String modifyFail03 = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        getScreenShot(getClass().getName());
        assert modifyFail03.equals("警告");
        //刷新页面
        driver.navigate().refresh();
    }


    //个人中心修改成功
    public void PersonalCenterModifySuc() throws InterruptedException, IOException {
        PersonalCenter();
        //清空当前昵称
        driver.findElement(By.cssSelector("#setting_input_nickname")).clear();
        //输入要修改的昵称
        driver.findElement(By.cssSelector("#setting_input_nickname")).sendKeys("阿巴阿巴4");
        //点击修改
        driver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        //提示修改成功
        String modifyNicknameSuc = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert modifyNicknameSuc.equals("成功");

        //清空当前邮箱
        driver.findElement(By.cssSelector("#setting_input_email")).clear();
        //输入要修改的邮箱
        driver.findElement(By.cssSelector("#setting_input_email")).sendKeys("275****82@qq.com");
        //点击修改
        driver.findElement(By.cssSelector("#setting_submit_email")).click();
        //提示修改成功
        String modifymailSuc = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert modifymailSuc.equals("成功");

        //清空当前电话号码
        driver.findElement(By.cssSelector("#setting_input_phoneNum")).clear();
        //输入要修改的电话号码
        driver.findElement(By.cssSelector("#setting_input_phoneNum")).sendKeys("151*****123");
        //点击修改
        driver.findElement(By.cssSelector("#setting_submit_phoneNum")).click();
        //提示修改成功
        String modifTelySuc = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert modifTelySuc.equals("成功");

        //个人简介
        driver.findElement(By.cssSelector("#settings_textarea_remark")).clear();
        driver.findElement(By.cssSelector("#settings_textarea_remark")).sendKeys("东海皇帝");
        driver.findElement(By.cssSelector("#settings_submit_remark")).click();
        //提示修改成功
        String modifTextSuc = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert modifTextSuc.equals("成功");


        //输入原密码
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("0123");
        //输入新密码
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("123");
        //输入新密码
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("123");
        //提交修改
        //driver.findElement(By.cssSelector("#settings_submit_password")).click();
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > div:nth-child(14) > div > div > form > div.form-footer>button")).click();

        getScreenShot(getClass().getName());

        //注册成功返回登录页面
        Thread.sleep(1000);
        String title = driver.getTitle();
        assert title.equals("比特论坛 - 用户登录");


    }

    //退出
    public void Exit(){
        //点击用户
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        //点击退出
        driver.findElement(By.cssSelector("#index_user_logout#index_user_logout")).click();

    }

}

